//
// ********************************************************************
// * License and Disclaimer                                           *
// *                                                                  *
// * The  Geant4 software  is  copyright of the Copyright Holders  of *
// * the Geant4 Collaboration.  It is provided  under  the terms  and *
// * conditions of the Geant4 Software License,  included in the file *
// * LICENSE and available at  http://cern.ch/geant4/license .  These *
// * include a list of copyright holders.                             *
// *                                                                  *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work  make  any representation or  warranty, express or implied, *
// * regarding  this  software system or assume any liability for its *
// * use.  Please see the license in the file  LICENSE  and URL above *
// * for the full disclaimer and the limitation of liability.         *
// *                                                                  *
// * This  code  implementation is the result of  the  scientific and *
// * technical work of the GEANT4 collaboration.                      *
// * By using,  copying,  modifying or  distributing the software (or *
// * any work based  on the software)  you  agree  to acknowledge its *
// * use  in  resulting  scientific  publications,  and indicate your *
// * acceptance of all terms of the Geant4 Software license.          *
// ********************************************************************
//
//
/// \file SpinTracking/src/SteppingAction.cc
/// \brief Implementation of the SpinTracking::SteppingAction class

#include "SteppingAction.hh"
#include "EventAction.hh"
#include "DetectorConstruction.hh"

#include "G4Step.hh"
#include "G4Proton.hh"
#include "G4AntiProton.hh"
#include "G4Lambda.hh"
#include "G4AntiLambda.hh"
#include "G4Deuteron.hh"
#include "G4Neutron.hh"
#include "G4SigmaMinus.hh"
#include "G4PionPlus.hh"
#include "G4PionZero.hh"
#include "G4He3.hh"
#include "G4Event.hh"
#include "G4RunManager.hh"
#include "G4LogicalVolume.hh"

namespace SpinTracking
{

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

SteppingAction::SteppingAction(EventAction* eventAction)
: fEventAction(eventAction)
{}

//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......

void SteppingAction::UserSteppingAction(const G4Step* step)
{

   // the Volume of last step
    G4LogicalVolume* preVolume = step->GetPreStepPoint()->GetTouchableHandle()->GetVolume()->GetLogicalVolume();
    
    // the Volume of next step 
    G4LogicalVolume* postVolume = step->GetPostStepPoint()->GetTouchableHandle()->GetVolume() ?
                                  step->GetPostStepPoint()->GetTouchableHandle()->GetVolume()->GetLogicalVolume() : nullptr;

    // check the valid of  preVolume and  postVolume
    if (preVolume && postVolume) {
        // select the particel from BeamPipeCore into  BeamPipe
        if (preVolume->GetName() == "BeamPipeCore" && postVolume->GetName() == "BeamPipe") {
            const G4Track* track = step->GetTrack();
            if (track) {
                G4ParticleDefinition* particleDef = track->GetDefinition();

                if (particleDef == G4Proton::Proton() && track->GetParentID() == 0) {
                    G4ThreeVector spin = track->GetPolarization(); 
                    fEventAction->AddSpin(spin.x(), spin.y(), spin.z());
                    G4ThreeVector position = track->GetPosition();
                
                // Abort the current event
                G4EventManager::GetEventManager()->AbortCurrentEvent(); 
                return; 
                }
            }
        }
    } else {
        if (postVolume == nullptr) {
            G4cout << "Particle is about to exit the World!" << G4endl;
             
        }
    }
}
}
